/*
 * Copyright (c) 2011 Nokia Corporation.
 */

#include "mainwindow.h"

#include <QtGui/QApplication>

#include <QtDeclarative/QDeclarativeContext>
#include <QtDeclarative/QDeclarativeEngine>

#include "levelcontroller.h"
#include "levelmodel.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    /***********************************************************************************************
    *Create a level object.                                                                        *
    *Load a level, terminate the program if loading fails.                                         *
    ***********************************************************************************************/
    Level level;
    if (!level.load())
        return 0;

    /***********************************************************************************************
    *Register the custom QML types as a part of the Shapes 1.0 library                             *
    ***********************************************************************************************/
    qmlRegisterType<EllipseItem>("Shapes", 1, 0, "Ellipse");
    qmlRegisterType<MultiTouchItem>("Shapes", 1, 0, "MultiTouchItem");

    /***********************************************************************************************
    *Create the custom QDeclarativeView subclass object.                                           *
    ***********************************************************************************************/
    View canvas;

    /***********************************************************************************************
    *Create a LevelController object passing it the games level object.                            *
    *This object is responsible for handling IAP.                                                  *
    *The controller emits the hack signal every time the IAP buy/restore sequence finalization,    *
    *needed in order to hide the software keys that have been shown in the process.                *
    ***********************************************************************************************/
    LevelController controller(&level);
    QObject::connect(&controller,SIGNAL(hack()),&canvas,SLOT(showFullScreen()));

    /***********************************************************************************************
    *Make level, canvas, controller and level model visible to the qml context.                    *
    ***********************************************************************************************/
    canvas.engine()->rootContext()->setContextProperty("Level",&level);
    canvas.engine()->rootContext()->setContextProperty("View",&canvas);
    canvas.engine()->rootContext()->setContextProperty("LevelController",&controller);
    canvas.engine()->rootContext()->setContextProperty("LevelModel",controller.getLevelModel());

    /***********************************************************************************************
    *Load the games main qml file.                                                                 *
    ***********************************************************************************************/
    canvas.setSource(QString("qrc:/qml/gameboard.qml"));

    /***********************************************************************************************
    *Lock the ui in landscape mode and show.                                                       *
    ***********************************************************************************************/
    canvas.setOrientation(View::ScreenOrientationLockLandscape);
    canvas.showExpanded();

    /***********************************************************************************************
    *The engines quit signal triggers the sounds to stop.                                          *
    *The levels readyToQuit signal triggers the apps quit slot                                     *
    ***********************************************************************************************/
    QObject::connect(canvas.engine(), SIGNAL(quit()), &level, SIGNAL(stopSounds()));
    QObject::connect(&level, SIGNAL(readyToQuit()), qApp, SLOT(quit()));

    return app.exec();
}
